home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / TOOLS.H < prev    next >
C/C++ Source or Header  |  1993-04-30  |  19KB  |  717 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #ifndef __Tools_H
  4. #define __Tools_H
  5.  
  6. #include "c2cpp.h"
  7. #include <stdio.h>
  8. #include <io.h>
  9. #include <dos.h>
  10.  
  11.  
  12. /* for Microsoft (or other than Borland) */
  13.  
  14. #ifndef MK_FP
  15. #define MK_FP( seg, ofs )   (void far *)( (unsigned long)((void far *)(seg)) + (unsigned long)((void near *)(ofs)) )
  16.  
  17. struct  fcb {
  18.     char    fcb_drive;      /* 0 = default, 1 = A, 2 = B */
  19.     char    fcb_name[8];    /* File name */
  20.     char    fcb_ext[3];     /* File extension */
  21.     short   fcb_curblk;     /* Current block number */
  22.     short   fcb_recsize;    /* Logical record size in bytes */
  23.     long    fcb_filsize;    /* File size in bytes */
  24.     short   fcb_date;       /* Date file was last written */
  25.     char    fcb_resv[10];   /* Reserved for DOS */
  26.     char    fcb_currec;     /* Current record in block */
  27.     long    fcb_random;     /* Random record number */
  28. };
  29.  
  30. struct  xfcb    {
  31.     char        xfcb_flag;    /* Contains 0xff to indicate xfcb */
  32.     char        xfcb_resv[5];  /* Reserved for DOS */
  33.     char        xfcb_attr;     /* Search attribute */
  34.     struct  fcb xfcb_fcb;      /* The standard fcb */
  35. };
  36.  
  37. #endif
  38.  
  39.  
  40.             
  41. typedef unsigned char BYTE;
  42.  
  43.  
  44. /***
  45.  *  Function    :  HIBYTE/LOWBYTE
  46.  *
  47.  *  Description :  Extract high/low byte of an integer
  48.  *
  49.  *  Parameters  :  in   int x
  50.  *
  51.  *  Side-effects:  Macro, so be careful.
  52.  *
  53.  *  Return      :  none
  54.  ***/
  55.  
  56. #define HIBYTE( x )   ( (unsigned int)(x) >> 8 )
  57. #define LOWBYTE( x )  ( (unsigned char)(x) )
  58.  
  59.  
  60. /***
  61.  *  Function    :  CAST
  62.  *
  63.  *  Description :  Cast any object to any type.
  64.  *
  65.  *  Parameters  :  in     new_type      any type
  66.  *                 in     object        any variable
  67.  *
  68.  *  Side-effects:  Macro, so be careful.
  69.  *
  70.  *  Return      :  none
  71.  ***/
  72.  
  73. #define CAST( new_type, object ) ( *((new_type *)&object) )
  74.      
  75.  
  76. /***
  77.  *  Function    :  SWAP
  78.  *
  79.  *  Description :  Swap two arguments of any type.
  80.  *
  81.  *  Parameters  :  in/out   x      any type argument
  82.  *                 in/out   y      any type argument
  83.  *
  84.  *  Side-effects:  Macro, so be careful.
  85.  *
  86.  *  Return      :  none
  87.  ***/
  88.  
  89. #define SWAP( x, y )        ( (x) ^= (y) ^= (x) ^= (y) )
  90.  
  91.  
  92. /***
  93.  *  Function    :  fileexist
  94.  *
  95.  *  Description :  Test if a filename exist
  96.  *
  97.  *  Parameters  :  in   char *filename
  98.  *
  99.  *  Return      :  0 or 1
  100.  ***/
  101.  
  102. #define fileexist( filename )   ( ! access(filename, 0) )
  103.  
  104.  
  105. /***
  106.  *  Function    :  ICA_get/put
  107.  *
  108.  *  Description :  Intra-application Communications Area get/put
  109.  *                 a 16 bytes data
  110.  *
  111.  *  Parameters  :  in   void *ptr   pointer to a 16 bytes data
  112.  *
  113.  *  Return      :  none
  114.  ***/
  115.  
  116. #define ICA_get( ptr )   ( memcpy(ptr, MK_FP(0x0040, 0x00F0), 16) )
  117. #define ICA_put( ptr )   ( memcpy(MK_FP(0x0040, 0x00F0), ptr, 16) )
  118.  
  119.  
  120.  
  121. /***
  122.  *  Function    :  getkey
  123.  *
  124.  *  Description :  Return a 2-bytes key pressed.
  125.  *
  126.  *  Parameters  :  none
  127.  *
  128.  *  Decisions   :  extended characters are added to 256.
  129.  *
  130.  *  Return code :  code of key pressed.
  131.  *
  132.  *  OS/Compiler :  MS-DOS
  133.  ***/
  134.  
  135. EXTERN int getkey( void );
  136.  
  137.  
  138. /***
  139.  *  Function    :  ungetkey
  140.  *
  141.  *  Description :  Puts a 2-bytes key into the keyboard buffer
  142.  *
  143.  *  Parameters  :  in    int key           key to be buffered
  144.  *
  145.  *  Decisions   :  extended characters are added to 256.
  146.  *
  147.  *  Return code :  none
  148.  *
  149.  *  Warning     :  May not work if a keyboard buffer extender is used.
  150.  *                 Does not handle the case of buffer full.
  151.  *
  152.  *  OS/Compiler :  MS-DOS
  153.  ***/
  154.  
  155. EXTERN void ungetkey( int key );
  156.  
  157.  
  158.  
  159. /***
  160.  *  Function    :  fnreduce
  161.  *
  162.  *  Description :  Transforms a filename into a full reduced filename.
  163.  *
  164.  *  Parameters  :  in/out   char *filename    input/reduced filename
  165.  *
  166.  *  Decisions   :  Translate into uppercase
  167.  *                  '/' are tranlated into '\\'
  168.  *
  169.  *  Return      :  pointer to result
  170.  *
  171.  *  OS/Compiler :  MS-DOS
  172.  ***/
  173.  
  174. EXTERN char *fnreduce( char *filename );
  175.  
  176.  
  177. /***
  178.  *  Function    :  getpgmpath
  179.  *
  180.  *  Description :  Transforms a filename into a full pathname
  181.  *                 relative to program directory.
  182.  *
  183.  *  Parameters  :  in/out   char *filename    input filename
  184.  *
  185.  *  Return      :  pointer to filename    
  186.  *
  187.  *  Precond     :  Global variable extern char **_argv must be defined
  188.  *                 and assign to argv in main( int argc, char *argv[] )
  189.  *                 Built-in in Borland
  190.  *                 Built-in in Microsoft (uses _pgmptr)
  191.  *
  192.  *  OS/Compiler :  MS-DOS version >= 3.0
  193.  ***/
  194.  
  195. EXTERN char *getpgmpath( char *filename );
  196.  
  197.  
  198. /***
  199.  *  Function    :  gettmppath
  200.  *
  201.  *  Description :  Transforms a filename into a full pathname
  202.  *                 relative to temporary directory.
  203.  *
  204.  *  Decisions   :  Temporary directory is first searched
  205.  *                 into environment variable 'TEMP',
  206.  *                 after into environment variable 'TMP'.
  207.  *                 If none are defined, program directory
  208.  *                 is assumed.
  209.  *
  210.  *  Parameters  :  in/out  char *filename    filename
  211.  *
  212.  *  Return      :  pointer to filename
  213.  *
  214.  *  Precond     :  Global variable extern char **_argv must be defined
  215.  *                 and assign to argv in main( int argc, char *argv[] )
  216.  *                 Built-in in Borland
  217.  *
  218.  *  OS/Compiler :  MS-DOS version >= 3.0
  219.  ***/
  220.  
  221. EXTERN char *gettmppath( char *filename );
  222.  
  223.  
  224. /***
  225.  *  Function    :  truename
  226.  *
  227.  *  Description :  Transforms a filename into a full reduced filename.
  228.  *                 Resolves all SUBST's, JOIN's, network names,...
  229.  *
  230.  *  Parameters  :  in/out   char *filename    input/reduced filename
  231.  *
  232.  *  Warning     :   - Valid only for DOS >= 2.0
  233.  *                  - Trailing '\' is not removed
  234.  *
  235.  *  Return      :  0      OK
  236.  *                 2  Invalid syntax
  237.  *                 3  Invalid drive or path
  238.  *
  239.  *  OS/Compiler :  MS-DOS version >= 2.0
  240.  ***/
  241.  
  242. EXTERN int truename( char *filename );
  243.  
  244.  
  245. /***
  246.  *  Function    :  exthandles
  247.  *
  248.  *  Description :  Allows MS-DOS program to open 255 handles
  249.  *
  250.  *  Parameters  :  none
  251.  *
  252.  *  Return      :  none
  253.  *
  254.  *  OS/Compiler :  MS-DOS version >= 3.30
  255.  ***/
  256.  
  257. EXTERN void exthandles( void );
  258.  
  259.  
  260.  
  261. /***
  262.  *  Function    :  getdensity
  263.  *
  264.  *  Description :  Return the density of a floppy drive.
  265.  *
  266.  *  Parameters  :  in    int drive         0 = A:, 1 = B:
  267.  *
  268.  *  Return      :  density in Kb (360, 720, 1200, 1440)
  269.  *                 0 if invalid drive or drive not ready
  270.  *
  271.  *  OS/Compiler :  MS-DOS
  272.  ***/
  273.  
  274. EXTERN int getdensity( int drive );
  275.  
  276.  
  277. /***
  278.  *
  279.  *  Function   :    test_drive
  280.  *
  281.  *  Topics     :    Test the availability of a drive.
  282.  *
  283.  *  Parameters :    in    int drive         0 = A:, 1 = B:
  284.  *
  285.  *  Return code:    0 if OK
  286.  *                  2 if not formatted
  287.  *                  3 if write-protected
  288.  *                  128 (0x80) if floppy not inserted
  289.  *                  other if not available
  290.  ***/
  291.  
  292. EXTERN int test_drive( int drive );
  293.  
  294. /* for compatibility with previous versions */
  295. #define is_drive_ready( drive )      ( ! test_drive(drive) )
  296.  
  297.  
  298.  
  299. /***
  300.  *
  301.  *  Function    :  geterrorlevel
  302.  *
  303.  *  Description :  Return errorlevel code from previous command executed
  304.  *                 by top COMMAND.COM
  305.  *
  306.  *  Parameters  :  none
  307.  *
  308.  *  Return      :  errorlevel
  309.  *                 -1 If MS-DOS version not supported
  310.  *
  311.  *  Warning     :  This function is only valid with COMMAND.COM from
  312.  *                 MS-DOS 3.20, 3.21, 3.30, 4.0, 4.01, 5.0 & NDOS 6.0.
  313.  */
  314.  
  315. EXTERN int geterrorlevel( void );
  316.  
  317.  
  318.  
  319. /***
  320.  *  Function    :  getdiskfree
  321.  *
  322.  *  Description :  Return the number of bytes free of a disk.
  323.  *
  324.  *  Parameters  :  in    int disk         0 = A:, 1 = B:, ...
  325.  *
  326.  *  Return      :  number of bytes free
  327.  *                 -1 if invalid drive
  328.  *
  329.  *  OS/Compiler :  MS-DOS
  330.  ***/
  331.  
  332. EXTERN long getdiskfree( int disk );
  333.  
  334.  
  335. /***
  336.  *  Function    :  setcwd
  337.  *
  338.  *  Description :  Change to specified directory accross disks
  339.  *
  340.  *  Parameters  :  in    char *dir    input directory name
  341.  *
  342.  *  Return      :  0      OK
  343.  *                 other  Invalid syntax, drive or path
  344.  *
  345.  *  OS/Compiler :  MS-DOS
  346.  ***/
  347.  
  348. EXTERN int setcwd( const char *dir );
  349.  
  350.  
  351. /***
  352.  *  Function    :  getsetup
  353.  *
  354.  *  Description :  Read paramaters in setup file.
  355.  *
  356.  *  Parameters  :  in    FILE *file        input file
  357.  *                 in    char *format      "keyword1=format keyword2=format"
  358.  *                 out   pointer list
  359.  *
  360.  *  Decisions   :  Expected format is a list of lines of the form
  361.  *
  362.  *                                 <keyword>=<value>
  363.  *
  364.  *                 - All letters are treated (and returned) as uppercases.
  365.  *                 - Blanks and tabs are skipped.
  366.  *
  367.  *                 - <value> can be enclosed between double quotes
  368.  *                    to preserve lowercases and blanks.
  369.  *
  370.  *                   - 2 consecutive double quotes allow to keep
  371.  *                   a double quote in <value>.
  372.  *
  373.  *                 - A number sign (#) in first column forces the line
  374.  *                   to be ignored.
  375.  *
  376.  *                 - The input formats in parameter 'format' are C standard
  377.  *                   ones ( %d, %s, %f,...).
  378.  *
  379.  *                 - Special format added: "%b" will translate
  380.  *                   True/False, Yes/No, On/Off, 1/0 into integer 1/0
  381.  *
  382.  *                 - Special format added: "%S" will result into copying
  383.  *                   all the input line (with spaces in it if any).
  384.  *                   It will not be processed by scanf-like processing.
  385.  *
  386.  *  Return      :  number of found parameters
  387.  *                 -1     syntax error
  388.  *                 -2     file error
  389.  *
  390.  *  Precond     :  Input file must be opened with read access.
  391.  *
  392.  *  OS/Compiler :  All
  393.  */
  394.  
  395. EXTERN int getsetup( FILE *file, const char *format, ... );
  396.  
  397.  
  398. /***
  399.  *  Function    :  load_options
  400.  *
  401.  *  Description :  Load options from a file.
  402.  *
  403.  *  Parameters  :  out  void * options     ptr to variable containing options
  404.  *                 in   size_t size        size of data to read
  405.  *
  406.  *  Decisions   :  The file has the same name as the program
  407.  *                 with extension .OPT and is searched in
  408.  *                 current directory, then in program directory.
  409.  *                 If option file is not found, options are not modified.
  410.  *
  411.  *  Return      :  number of bytes read
  412.  *                 -1 if file was not found
  413.  *
  414.  *  Precond     :  Global variable extern char **_argv must be defined
  415.  *                 and assign to argv in main( int argc, char *argv[] )
  416.  *                 Built-in in Borland
  417.  *
  418.  *  OS/Compiler :  MS-DOS version >= 3.0
  419.  ***/
  420.  
  421. EXTERN int load_options( void *options, size_t size );
  422.  
  423. /***   To not specify the size   ***/
  424. #define Opt_load( opt )     load_options( (opt), sizeof(*(opt)) )
  425.  
  426.  
  427.  
  428.  
  429. /***
  430.  *  Function    :  save_options
  431.  *
  432.  *  Description :  Save options in a file.
  433.  *
  434.  *  Parameters  :  in  void * options     ptr to variable containing options
  435.  *                 in  size_t size        size of data to read
  436.  *
  437.  *  Decisions   :  If options were previously loaded, this file is used.
  438.  *                 Otherwise, the file has the same name as the program
  439.  *                 with extension .OPT and is located in program directory.
  440.  *
  441.  *  Return      :  number of bytes written
  442.  *                 -1 if file not opened
  443.  *
  444.  *  Precond     :  Global variable extern char **_argv must be defined
  445.  *                 and assign to argv in main( int argc, char *argv[] )
  446.  *                 Built-in in Borland
  447.  *
  448.  *  OS/Compiler :  MS-DOS version >= 3.0
  449.  ***/
  450.  
  451. EXTERN int save_options( const void *options, size_t size );
  452.  
  453. /***   To not specify the size   ***/
  454. #define Opt_save( opt )     save_options( (opt), sizeof(*(opt)) )
  455.  
  456.  
  457. /***
  458.  *
  459.  *  Function    :  load_cfg
  460.  *
  461.  *  Description :  Load configuration from a file.
  462.  *
  463.  *  Parameters :   in   char * name        filename
  464.  *                 out  void * config      ptr to variable containing configuration
  465.  *                 in   size_t size        size of data to read
  466.  *
  467.  *  Decisions   :  The file has the same name as the parameter 'name'
  468.  *                 but with extension .CFG
  469.  *                 If configuration file is not found, config is unchanged.
  470.  *
  471.  *  Return     :   number of bytes read
  472.  *                 -1 if file was not found
  473.  *
  474.  *  OS/Compiler :  MS-DOS
  475.  ***/
  476.  
  477. EXTERN int load_cfg( const char *name, void *config, size_t bsize );
  478.  
  479. /***   To not specify the size   ***/
  480. #define Cfg_load( name, cfg )   load_cfg( (name), (cfg), sizeof(*(cfg)) )
  481.  
  482.  
  483.  
  484. /***
  485.  *
  486.  *  Function    :  save_cfg
  487.  *
  488.  *  Description :  Save a configuration into a file.
  489.  *
  490.  *  Parameters  :  in   char * name        filename
  491.  *                 in   void * config      ptr to variable containing configuration
  492.  *                 in   size_t size        size of data to write
  493.  *
  494.  *  Decisions   :  The file has the same name as the parameter 'name'
  495.  *                 but with extension .CFG
  496.  *
  497.  *  Return      :  number of bytes written
  498.  *                 -1 if file was not found
  499.  *
  500.  *  OS/Compiler :  MS-DOS
  501.  ***/
  502.  
  503. EXTERN int save_cfg( const char *name, const void *config, size_t bsize );
  504.  
  505. /***   To not specify the size   ***/
  506. #define Cfg_save( name, cfg )   save_cfg( (name), (cfg), sizeof(*(cfg)) )
  507.  
  508.  
  509.  
  510. /***
  511.  *  Function    :  filencopy
  512.  *
  513.  *  Description :  Copy n characters from a file on another.
  514.  *
  515.  *  Parameters  :  in   int            target    target file descriptor
  516.  *                 in   int            source    source file descriptor
  517.  *                 in   unsigned long  length    number of bytes to copy
  518.  *
  519.  *  Precond     :  The two files must be opened.
  520.  *
  521.  *  Decisions   :  The copy can stop when eof is reached.
  522.  *
  523.  *  Warning     :  no rewind is performed on either file!!!
  524.  *
  525.  *  Return      :  0 if OK
  526.  *                 errno if set
  527.  *                 -1 otherwise
  528.  *
  529.  *  OS/Compiler :  All
  530.  ***/
  531.  
  532. EXTERN int filencopy( int source, int target, unsigned long length );
  533.  
  534. /* maximum size */
  535. #define filecopy( source, target )    filencopy( source, target, 0xFFFFFFFF )
  536.  
  537.  
  538. /***
  539.  *  Function    :  Filencopy
  540.  *
  541.  *  Description :  Copy n characters from a file on another.
  542.  *
  543.  *  Parameters  :  in   char *         source    source filename
  544.  *                 in   char *         target    target filename
  545.  *                 in   unsigned long  length    number of bytes to copy
  546.  *
  547.  *  Decisions   :  The copy can stop when eof is reached.
  548.  *
  549.  *  Return      :  0 if OK
  550.  *                 errno if set
  551.  *                 -1 otherwise
  552.  *
  553.  *  OS/Compiler :  All
  554.  ***/
  555.  
  556. EXTERN int Filencopy( const char *source, const char *target, unsigned long length );
  557.  
  558. /* maximum size */
  559. #define Filecopy( source, target )    Filencopy( source, target, 0xFFFFFFFF )
  560.  
  561.  
  562.  
  563.  
  564. /***
  565.  *  Function    :  Filecat
  566.  *
  567.  *  Description :  Concatenate two files on another.
  568.  *
  569.  *  Parameters  :  in   char *     newpath     result filename
  570.  *                 in   char *     path1       target filename
  571.  *                 in   char *     path2       source filename
  572.  *
  573.  *  Return      :  0 if OK
  574.  *                 errno if set
  575.  *                 -1 otherwise
  576.  *
  577.  *  OS/Compiler :  All
  578.  ***/
  579.  
  580. EXTERN int Filecat( const char *newpath, const char *path1, const char *path2 );
  581.  
  582.  
  583. /***
  584.  *  Function    :  filetrunc
  585.  *
  586.  *  Description :  Truncate a file to a specified length.
  587.  *
  588.  *  Parameters  :  in   int            fd        file descriptor
  589.  *                 in   unsigned long  length    number of bytes to copy
  590.  *
  591.  *  Precond     :  The file must be opened with writing permission
  592.  *
  593.  *  Warning     :  no rewind is performed on the file!!!
  594.  *
  595.  *  Return      :  0 if OK
  596.  *                 errno if error
  597.  *
  598.  *  OS/Compiler :  All
  599.  ***/
  600.  
  601. EXTERN int filetrunc( int fd, unsigned long length );
  602.  
  603.  
  604.  
  605.  
  606. /***
  607.  *  Function    :  Filetrunc
  608.  *
  609.  *  Description :  Truncate a file to a specified length.
  610.  *
  611.  *  Parameters  :  in   char *         path      file descriptor
  612.  *                 in   unsigned long  length    number of bytes to copy
  613.  *
  614.  *  Return      :  0 if OK
  615.  *                 errno if error
  616.  *
  617.  *  OS/Compiler :  All
  618.  ***/
  619.  
  620. EXTERN int Filetrunc( const char *path, int length );
  621.  
  622.  
  623.  
  624.  
  625. /***
  626.  *  Functions   :  filedel
  627.  *
  628.  *  Description :  Like 'unlink' but allows wildcards
  629.  *
  630.  *  Parameters  :  in   char *  filename   pathname with/without wildcards
  631.  *
  632.  *  Return      :  none
  633.  *
  634.  *  OS/Compiler :  MS-DOS
  635.  ***/
  636.  
  637. EXTERN void filedel( const char *filename );
  638.  
  639.  
  640.  
  641. /***
  642.  *  Function    :  getPasswd
  643.  *
  644.  *  Description :  Input a password (echoes '*')
  645.  *
  646.  *  Parameters  :  in  char *passwd    string to store password
  647.  *
  648.  *  Decisions   :  Echoes '*' in place of characters.
  649.  *                  Backspace and left arrow can be used to erase characters
  650.  *                  Characters must be in the range 32 - 254
  651.  *
  652.  *  Return      :  pointer to password
  653.  *
  654.  *  OS/Compiler :  MS-DOS & Turbo-C
  655.  ***/
  656.  
  657. EXTERN char *getPasswd( char *passwd );
  658.  
  659.  
  660.  
  661. /***
  662.  *  Function    :  vollabel
  663.  *
  664.  *  Description :  Get/Set disk volume label
  665.  *
  666.  *  Parameters  :  in       int drive         0 = A:, 1 = B:, ...
  667.  *                 in/out   char *vol         volume label
  668.  *
  669.  *  Decisions   :  - If *vol = '\0', it is filled with disk volume label.
  670.  *                 - Otherwise, vol is copied onto disk volume label.
  671.  *
  672.  *  Return      :   0 if OK
  673.  *                 -1 if error
  674.  ***/
  675.  
  676. EXTERN int vollabel( int drive, char *vol );
  677.  
  678.  
  679. #if ! defined(__TURBOC__)
  680. /*  void sound ( short frequency, long duration )
  681.  *
  682.  *  Makes the PC speaker emit a sound at `frequency' Herz for approximately
  683.  *  `duration' milliseconds.
  684.  */
  685. EXTERN void sound ( short frequency, long duration );
  686. #endif
  687.  
  688. /***
  689.  *
  690.  *  Function beep :   Emit a beep.
  691.  *
  692.  ***/
  693. EXTERN void beep( void );
  694.  
  695. /***
  696.  *
  697.  *  Function buzzer:   Emit a buzzer sound.
  698.  *
  699.  ***/
  700. EXTERN void buzzer( void );
  701.  
  702.  
  703. /***
  704.  *  Function    :  cpu_type
  705.  *
  706.  *  Description :  returns cpu type (86, 186, 286, 386, 486)
  707.  *
  708.  *  Parameters  :  none
  709.  *
  710.  *  Return      :  86, 186, 286, 386, 486
  711.  ***/
  712.  
  713. EXTERN int far cpu_type( void );
  714.  
  715.  
  716. #endif
  717.